import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from "path";

import components from 'unplugin-vue-components/vite';
import { AntDesignXVueResolver } from 'ant-design-x-vue/resolver';

// https://vite.dev/config/
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    // 设置scss的api类型为modern-compiler
    css: {
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler'
        }
      }
    },

    plugins: [vue(),
      components({
        resolvers: [AntDesignXVueResolver()]
      })
    ],
    // server: {
    //   host: "0.0.0.0",
    //   port: 5180,
    //   proxy: {
    //     [env.VITE_APP_BASE_API]: {
    //       target: env.VITE_API_BASE_URL,
    //       secure: false, // 请求是否为https
    //       changeOrigin: true, // 是否跨域
    //       // rewrite: (path) => path.replace(/^\/api/, '')
    //     },
    //   },
    // },
    resolve: {
      alias: {
        // 设置路径
        '~': resolve(__dirname, './'),
        // 设置别名
        '@': resolve(__dirname, './src')
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    build: {
      chunkSizeWarningLimit: 1600, // 打包文件大小限制
      rollupOptions: { // 打包配置
        output: { //
          manualChunks(id) { // 手动分包
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString(); // 根据node_modules中的包名进行分包
            }
          }
        }
      }
    },
  };
});
